package netbay;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class WorkerManager {


	public static String TCDRecvCmd = "RecvTCD";
	public static String JPMCRecvCmd = "RecvTCD";
	public static String testCmd = "Test";

	public static String TCDRecvDir = "";
	public static String JPMCRecvDir = "";
	public static String JPMCDesDir = "";

	
	// use to prevent from duplicate task
	// it's a queue board to show there is a process on which filename
	private static List<String> assignedTCDId;
	private static List<String> assignedJPMCId;
	private static List<String> assignedTestId;
	
	
	public static void assignJob(String cmd) {
		
//		PropertyConfigurator.configure("log4j.properties");
		

		
		

		if (cmd.equalsIgnoreCase(TCDRecvCmd)) {

			recvTCD();

		} else if (cmd.equalsIgnoreCase(JPMCRecvCmd)) {

			recvJPMC();

		} else if (cmd.equalsIgnoreCase(testCmd)) {

			System.out.println("get test");

		}

	}

//	private static void testSend() {
//
//		System.out.println("perform test");
//
//		File testDir = new File(WorkerManager.TCDRecvDir);
//
//		String[] fileList = testDir.list();
//
//		SFTPManager manager = new SFTPManager();
//
//		for (int i = 0; i < fileList.length; ++i) {
//
//			System.out.println(WorkerManager.TCDRecvDir + "/" + fileList[i]);
//			
//			manager.sendSFTPFile(new File(WorkerManager.TCDRecvDir
//					+ "/" + fileList[i]), fileList[i]);
//		}
//
//	}

	private static void recvJPMC() {

		SFTPManager manager = new SFTPManager();
		List<String> fileList = null;
		fileList = manager.lsForFiles();

		if (fileList == null || fileList.size() == 0)
			System.out.println("none of files");
		else {
			for (int i = 0; i < fileList.size(); ++i) {

				String fileName = fileList.get(i);

				// need to check before start -- operate only attach file
				// operate on attach file and self-generate ctrl file
				if (fileName
						.substring(fileName.length() - 4, fileName.length())
						.equalsIgnoreCase(".xml")) {

					String attachFileName = fileName;

					if (checkId(attachFileName, JPMCRecvCmd)) {

						Worker myWorker = new Worker(attachFileName,
								JPMCRecvCmd);
						Thread working = new Thread(myWorker);
						working.start();

					}

				}

			}
		}
	}

	private static void recvTCD() {

		System.out.println("check TCDdirectory");

		FileDao fileMng = new FileDao();

		// init data must change to load from config file
		String TCDStorePath = TCDRecvDir;
		File expFolder = new File(TCDStorePath);
		String[] fileList = fileMng.exploreFolder(expFolder);

		if (fileList.length == 0)
			System.out.println("none of files");
		else {
			for (int i = 0; i < fileList.length; ++i) {

				String fileName = fileList[i];

				// need to check before start -- operate only ctrl file
				// operate on attach file and ctrl file
				if (fileName
						.substring(fileName.length() - 5, fileName.length())
						.equalsIgnoreCase(".ctrl")) {

					String ctrlFileName = fileName;
					
					System.out.println("found " + fileName);

					if (checkId(ctrlFileName, TCDRecvCmd)) {

						Worker myWorker = new Worker(ctrlFileName, TCDRecvCmd);
						Thread working = new Thread(myWorker);
						working.start();

					}

				}

			}
		}
	}

	public static boolean checkId(String fileName, String cmd) {

		List<String> checkList = null;

		if (cmd.equalsIgnoreCase(TCDRecvCmd)) {
			if (assignedTCDId == null)
				assignedTCDId = new ArrayList<String>();

			checkList = assignedTCDId;
		}
		if (cmd.equalsIgnoreCase(JPMCRecvCmd)) {
			if (assignedJPMCId == null)
				assignedJPMCId = new ArrayList<String>();

			checkList = assignedJPMCId;
		}
		if (cmd.equalsIgnoreCase(testCmd)) {
			if (assignedTestId == null)
				assignedTestId = new ArrayList<String>();

			checkList = assignedTestId;
		}

		if (!checkList.contains(fileName)) {
			checkList.add(fileName);
			return true;
		} else
			return false;
	}

	public static void reportId(String fileName, String cmd) {
		List<String> checkList = null;

		if (cmd.equalsIgnoreCase(TCDRecvCmd)) {
			checkList = assignedTCDId;
		}
		if (cmd.equalsIgnoreCase(JPMCRecvCmd)) {
			checkList = assignedJPMCId;
		}
		if (cmd.equalsIgnoreCase(testCmd)) {
			checkList = assignedTestId;
		}

		if (checkList.contains(fileName)) {
			checkList.remove(fileName);
		}
	}

}
